package cn.xinjiu.reggie.config;

import cn.xinjiu.reggie.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * Created by xinchen on 2022/7/3
 *
 * @Description : web-mvc的配置
 */
@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * @param converters
     * @return
     * @description 扩展mvc的消息转换器
     *     目的是把Long型ID转换为字符串，解决丢失精度的问题
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转换器.....");
        // 创建消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        // 设置转换器的转换规则
        converter.setObjectMapper(new JacksonObjectMapper());
        // 添加到消息转换器列表中,设置默认的消息转换器
        converters.add(0, converter);
    }
}
